package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.ISapDeliveryNoteMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * SAP交货单表头 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/sapDeliveryNoteM")
@Api(tags = "SAP DN交货单—贸易货分批", description = "SAP DN贸易货交货单接口")
public class SapDeliveryNoteMController {

    @Autowired
    ISapDeliveryNoteMService iSapDeliveryNoteMService;

    @RequestMapping("/saveSapDNDtl")
    @ApiOperation(value = "贸易货收货保存/提交SAP", notes = "贸易货分批次收货保存\n" +
            "{\n" +
            "\t\"sapDeliveryNoteDtls\": [{\n" +
            "\t\t\t\"documentDate\": null,\n" +
            "\t\t\t\"materialNo\": \"210030763\",\n" +
            "\t\t\t\"qtySales\": null,\n" +
            "\t\t\t\"deliveryNoteSapItem\": 10,\n" +
            "\t\t\t\"postingDate\": null,\n" +
            "\t\t\t\"itemStatus\": \"0\",\n" +
            "\t\t\t\"whsLocationCode\": \"1011\",\n" +
            "\t\t\t\"actualQtySales\": null,\n" +
            "\t\t\t\"updateTime\": \"2021-08-09 02:35:05\",\n" +
            "\t\t\t\"tempStorage\": null,\n" +
            "\t\t\t\"spmsItem\": 10,\n" +
            "\t\t\t\"werks\": \"CN01\",\n" +
            "\t\t\t\"receivedQty\": null,\n" +
            "\t\t\t\"deliveryNoteSap\": \"10000\",\n" +
            "\t\t\t\"remarks\": null,\n" +
            "\t\t\t\"operator\": \"张三\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"documentDate\": null,\n" +
            "\t\t\t\"materialNo\": \"130012803\",\n" +
            "\t\t\t\"qtySales\": null,\n" +
            "\t\t\t\"deliveryNoteSapItem\": 20,\n" +
            "\t\t\t\"postingDate\": null,\n" +
            "\t\t\t\"itemStatus\": \"0\",\n" +
            "\t\t\t\"whsLocationCode\": \"1011\",\n" +
            "\t\t\t\"actualQtySales\": null,\n" +
            "\t\t\t\"updateTime\": \"2021-08-09 02:35:05\",\n" +
            "\t\t\t\"tempStorage\": null,\n" +
            "\t\t\t\"spmsItem\": 20,\n" +
            "\t\t\t\"werks\": \"CN01\",\n" +
            "\t\t\t\"receivedQty\": null,\n" +
            "\t\t\t\"deliveryNoteSap\": \"10000\",\n" +
            "\t\t\t\"remarks\": null,\n" +
            "\t\t\t\"operator\": \"张三\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"documentDate\": null,\n" +
            "\t\t\t\"materialNo\": \"130000131\",\n" +
            "\t\t\t\"qtySales\": null,\n" +
            "\t\t\t\"deliveryNoteSapItem\": 30,\n" +
            "\t\t\t\"postingDate\": null,\n" +
            "\t\t\t\"itemStatus\": \"0\",\n" +
            "\t\t\t\"whsLocationCode\": \"1011\",\n" +
            "\t\t\t\"actualQtySales\": null,\n" +
            "\t\t\t\"updateTime\": \"2021-08-09 02:35:05\",\n" +
            "\t\t\t\"tempStorage\": null,\n" +
            "\t\t\t\"spmsItem\": 30,\n" +
            "\t\t\t\"werks\": \"CN01\",\n" +
            "\t\t\t\"receivedQty\": null,\n" +
            "\t\t\t\"deliveryNoteSap\": \"10000\",\n" +
            "\t\t\t\"remarks\": null,\n" +
            "\t\t\t\"operator\": \"张三\"\n" +
            "\t\t},\n" +
            "\t\t{\n" +
            "\t\t\t\"documentDate\": null,\n" +
            "\t\t\t\"materialNo\": \"130000131\",\n" +
            "\t\t\t\"qtySales\": null,\n" +
            "\t\t\t\"deliveryNoteSapItem\": 40,\n" +
            "\t\t\t\"postingDate\": null,\n" +
            "\t\t\t\"itemStatus\": \"0\",\n" +
            "\t\t\t\"whsLocationCode\": \"1011\",\n" +
            "\t\t\t\"actualQtySales\": null,\n" +
            "\t\t\t\"updateTime\": \"2021-08-09 02:35:05\",\n" +
            "\t\t\t\"tempStorage\": null,\n" +
            "\t\t\t\"spmsItem\": 40,\n" +
            "\t\t\t\"werks\": \"CN01\",\n" +
            "\t\t\t\"receivedQty\": null,\n" +
            "\t\t\t\"deliveryNoteSap\": \"10000\",\n" +
            "\t\t\t\"remarks\": null,\n" +
            "\t\t\t\"operator\": \"张三\"\n" +
            "\t\t}\n" +
            "\t],\n" +
            "\t\"sapDN\": {\n" +
            "\t\t\"deliveryNoteSap\": \"10000\",\n" +
            "\t\t\"updateTime\": \"2021-08-09 01:32:14\"\n" +
            "\t},\n" +
            "\t\"targetStatus\": \"3\" \n" +
            "}\n" +
            "交货单贸易货行项目状态（默认0-待到货；3-收货中；7-提交SAP失败；8-提交SAP完成；11-删除/关闭；12-已撤销；）\n" +
            "1.根据交货单更新时间判断当前数据是否最新版本；不是则提示错误；\n" +
            "2.此接口忽略行数据状态为 8、11、12 的数据；targetStatus：3表示保存；8表示提交SAP；\n" +
            "3.保存功能：根据前端提交数据，以及更新时间判断是否最新数据，更新状态为 0、3、7 全部行数据当前状态；\nSPMS行号字符长度大于6为新增行数据，新增行数据状态为 3；\n" +
            "4.提交SAP功能：先按步骤2执行保存功能；然后将送货单下状态为 3、7 的 RECEIVED_QTY 大于0的行查询出来提交SAP；\n" +
            "    4.1.提交成功：更新这些行数据库存、记录单据日志、记录提交SAP日志、更新这些行状态为 8-提交SAP完成.\n" +
            "    4.2.提交失败：忽略库存更新、记录单据日志、记录提交SAP日志、更新这些行状态为 7-提交SAP失败；返回SAP错误信息给前端；\n" +
            "    4.3.提交异常：忽略库存更新、忽略单据日志等日志记录；返回异常信息给前端；\n" +
            "5.更新交货单的 SPMS_STATUS = 3-收货中；\n" +
            "6.返回该交货单数据库最新版的 交货单表头、交货单明细 最新版数据；\n",
            httpMethod = "POST")
    public ResponseResult getSapDnLog(@RequestBody JSONObject doc) {
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSapDeliveryNoteMService.saveSapDeliveryNote(doc, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error().add("error", e.getMessage());
        }
    }


}
